﻿// ***********************************************************************
// Project          : Micua
// Assembly         : Micua.Core
// Author           : Administrator
// Created          : 2013-12-29 5:48 PM
// 
// Last Modified By : Administrator
// Last Modified On : 2013-12-29 5:48 PM
// ***********************************************************************
// <copyright file="ViewEngine.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************

using System.Collections.Generic;
using System.Web.Mvc;
using Micua.Core.Configuration;
using Micua.Model.Struct;

namespace Micua.UI.TEngine
{
    /// <summary>
    /// 前台视图引擎
    /// </summary>
    public class MiucaViewEngine  : BuildManagerViewEngine
    {
        /// <summary>
        /// 模版引擎构造
        /// </summary>
        public MiucaViewEngine() : this(null) { }

        /// <summary>
        /// 模版引擎构造
        /// </summary>
        public MiucaViewEngine(IViewPageActivator viewPageActivator)
            : base(viewPageActivator)
        {
            #region 移到配置文件

            const string themeRoot = "~/Content/Themes/";
            string themeName = Setting.GetString(OptionKeys.SiteTheme);

            AreaViewLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            AreaMasterLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            AreaPartialViewLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            ViewLocationFormats = new[]
            {
                themeRoot + themeName + "/Views/{0}.cshtml", 
                themeRoot + themeName + "/Views/{0}.vbhtml", 
                themeRoot + themeName + "/Views/{1}/{0}.cshtml", 
                themeRoot + themeName + "/Views/{1}/{0}.vbhtml", 
                themeRoot + themeName + "/Views/Shared/{0}.cshtml", 
                themeRoot + themeName + "/Views/Shared/{0}.vbhtml"
            };
            MasterLocationFormats = new[]
            {
                themeRoot + themeName + "/Views/{0}.cshtml", 
                themeRoot + themeName + "/Views/{0}.vbhtml", 
                themeRoot + themeName + "/Views/{1}/{0}.cshtml", 
                themeRoot + themeName + "/Views/{1}/{0}.vbhtml", 
                themeRoot + themeName + "/Views/Shared/{0}.cshtml", 
                themeRoot + themeName + "/Views/Shared/{0}.vbhtml"
            };
            PartialViewLocationFormats = new[]
            {
                themeRoot + themeName + "/Views/{0}.cshtml", 
                themeRoot + themeName + "/Views/{0}.vbhtml", 
                themeRoot + themeName + "/Views/{1}/{0}.cshtml", 
                themeRoot + themeName + "/Views/{1}/{0}.vbhtml", 
                themeRoot + themeName + "/Views/Shared/{0}.cshtml", 
                themeRoot + themeName + "/Views/Shared/{0}.vbhtml"
            };
            FileExtensions = new[] { "cshtml", "vbhtml" }; 

            #endregion
        }
        /// <summary>
        /// 创建局部视图
        /// </summary>
        /// <param name="controllerContext"></param>
        /// <param name="partialPath"></param>
        /// <returns></returns>
        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            IEnumerable<string> fileExtensions = FileExtensions;
            return new RazorView(controllerContext, partialPath, null, false, fileExtensions, ViewPageActivator);
        }
        /// <summary>
        /// 创建视图
        /// </summary>
        /// <param name="controllerContext"></param>
        /// <param name="viewPath"></param>
        /// <param name="masterPath"></param>
        /// <returns></returns>
        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            string layoutPath = masterPath;
            IEnumerable<string> fileExtensions = FileExtensions;
            return new RazorView(controllerContext, viewPath, layoutPath, true, fileExtensions, ViewPageActivator);
        }
    }
}